package net.pzc.filechooser;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class MyComboBoxRenderer extends JPanel implements ListCellRenderer {
	private static final long serialVersionUID = 1L;
	private JComboBox ui = null;

	public MyComboBoxRenderer(JComboBox ui) {
		this.ui = ui;
		setOpaque(true);
		// setHorizontalAlignment(CENTER);
		// setVerticalAlignment(CENTER);
	}

	/*
	 * This method finds the image and text corresponding to the selected value
	 * and returns the label, set up to display the text and image.
	 */
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
			boolean cellHasFocus) {

		// list.setSelectionForeground(Color.white);
		// list.setSelectionBackground(new Color(10, 36, 106));

		if (value != null) {

			this.setLayout(null);
			this.removeAll();

			JLabel lbl = new JLabel() {
				private static final long serialVersionUID = 1L;

				@Override
				public Dimension getPreferredSize() {
					// 根据字体自适应宽度
					String text = getText();
					Dimension ret = new Dimension(1, 20);
					Font font = getFont();
					if (font != null) {
						FontMetrics fm = this.getFontMetrics(getFont());
						int w = fm.stringWidth(text) + 20;
						// w += (text.length() - 1) * tracking;
						// w += left_x + right_x;

						int h = fm.getHeight();
						// h += left_y + right_y;
						h += 1;

						ret.width = w + 6;
						// ret.height = h;
					}

					return ret;
				}

				@Override
				public void setText(String text) {
					super.setText(text);
					setSize(getPreferredSize());
					revalidate();
				}
			};

			if (isSelected && ui.isPopupVisible()) {
				// setBackground(list.getSelectionBackground());
				// setForeground(list.getSelectionForeground());
				setBackground(new Color(10, 36, 106));
				lbl.setForeground(Color.white);
			} else {
				setBackground(list.getBackground());
				lbl.setForeground(list.getForeground());
			}

			lbl.setBorder(null);
			FileItem fi = (FileItem) value;
			// JLabel lbl = new JLabel();
			lbl.setText(fi.getName());
			// this.add(lbl);
			// lbl.setSize(30, 20);
			if (fi.getIcon() != null) {
				lbl.setIcon(new ImageIcon(fi.getIcon()));
			}
			// System.out.println(lbl);
			lbl.setLocation(fi.getStep() * 10, 0);
			lbl.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0));
			this.add(lbl);

		}

		return this;
	}

	@Override
	public Dimension getPreferredSize() {
		// return super.getPreferredSize();
		int maxW = -1;
		int maxH = -1;
		for (int i = 0; i < this.getComponentCount(); i++) {
			Component c = this.getComponent(i);
			maxW = Math.max(maxW, c.getLocation().x + c.getWidth());
			maxH = Math.max(maxH, c.getLocation().y + c.getHeight());
		}
		return new Dimension(maxW, maxH);
	}
}
